package edu.nf.ch06;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.stereotype.Service;

/**
 * @Author gjh
 * @Date 2025/10/9
 * @Description: 注册（消息生产者）
 */
@Service
public class Egister {

    private static final String QUEUE_NAME = "code";

    public static  ConnectionFactory connectionFactory;

    static {
        connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        // 5672 是本机的端口号
        connectionFactory.setPort(5672);
    }

    public static void send(String message){
        try(Connection conn = connectionFactory.newConnection()){
            Channel channel = conn.createChannel();
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        } catch (Exception e) {
            throw new RuntimeException("操作失败"+e);
        }
    }
}
